<?php

namespace Home\Model;

use Think\Model;

class MemberModel extends Model {

    /**
     * 执行登录相关的赋值操作
     */
    function do_login($user_id) {
        //根据用户ID查询
        $user_info = $this->field('user_id, user_name, reg_time, last_login, last_ip, store_id, available')->join('LEFT JOIN ' . C('DB_PREFIX') . 'store  ON ' . C('DB_PREFIX') . 'store.store_id = ' . C('DB_PREFIX') . 'member.user_id')->where('user_id=' . $user_id)->find();
        if ($user_info) {
            return $user_info;
        }else{
            render('10106', '用户不存在');
        }
    }

    /**
     * 验证用户登录
     */
    function do_auth($user_name, $password) {
        $info = $this->where('user_name="' . $user_name . '"')->find();
        if ($info['password'] != md5($password)) {
            render('10107', '登录密码错误');
        }
        return $info['user_id'];
    }

    function do_register($user_name, $password='', $email='', $local_data = array()) {
        if (!$this->check_username($user_name)) {
            return false;
        }

        if (!$this->check_email($email)) {
            return false;
        }

        if (!$this->check_mobile($local_data['phone_mob'])) {
            return false;
        }
        $local_data['user_name'] = $user_name;
        $password && $local_data['password'] = md5($password);
        $local_data['email'] = $email;
        $local_data['reg_time'] = time();

        $user_id = $this->add($local_data);

        return $user_id;
    }
    
    function do_edit($user_id, $old_password, $items, $force = false)
    {
        $info = $this->where('user_id='.$user_id)->find();
        if (!$force)
        {
            if (md5($old_password) != $info['password'])
            {
                render('10107', '登录密码错误');
            }
        }
        if ($info['email'] != $items['email']) {
            if (!$this->check_email($items['email'])) {
                return false;
            }
        }
        if ($info['phone_mob'] != $items['phone_mob']) {
            if (!$this->check_mobile($items['phone_mob'])) {
                return false;
            }
        }
        $edit_data = array();
        if (isset($items['password']))
        {
            $edit_data['password']  = md5($items['password']);
        }
        if (isset($items['email']))
        {
            $edit_data['email'] = $items['email'];
        }
        if (isset($items['phone_mob']))
        {
            $edit_data['phone_mob'] = $items['phone_mob'];
        }
        if (empty($edit_data))
        {
            return false;
        }
        $this->where('user_id='.$user_id)->save($edit_data);
    }
    

    /**
     *    检查电子邮件是否唯一
     *
     *    @author    Garbin
     *    @param     string $email
     *    @return    bool
     */
    function check_email($email) {
        /* 暂时无此设置 */
        if (empty($email)) {
            return true;
        }
        //验证是否为邮箱
        if (!preg_match("/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i", $email)) {
            render('10004', '邮箱格式错误');
        }
        $info = $this->where("email='{$email}' or user_name='{$email}'")->find();
        if (!empty($info)) {
            render('10108', '邮箱已存在');
        }
        return true;
    }

    /**
     *    检查用户名是否唯一
     *
     *    @author    Garbin
     *    @param     string $user_name
     *    @return    bool
     */
    function check_username($user_name) {
        $info = $this->where("user_name='{$user_name}' or phone_mob='{$user_name}' or email='{$user_name}'")->find();
        if (!empty($info)) {
            render('10109', '用户已存在');
        }
        return true;
    }

    /**
     *    检查手机号码是否唯一
     *
     *    @author    Garbin
     *    @param     string $mobile
     *    @return    bool
     */
    function check_mobile($mobile) {
        if (empty($mobile)) {
            return true;
        }
        //验证是否为手机号
        if (!preg_match("/^1[34578]\d{9}$/", $mobile)) {
            render('10003', '手机号格式错误');
        }
        $info = $this->where("phone_mob='{$mobile}' or user_name='{$mobile}'")->find();
        if (!empty($info)) {
            render('10110', '手机号已存在');
        }
        return true;
    }
    
    
    /*
     * 获取用户的等级信息
     */
    function get_grade_info($user_id) {
        if (!$user_id) {
            return;
        }
        $user = $this->where('user_id='.$user_id)->find();
        
        if (!$user) {
            return;
        }
        $Ugrade_mod = M('ugrade');
        $ugrade = $Ugrade_mod->where('grade=' . $user['ugrade'])->find();
        $higher_grade = $Ugrade_mod->where('grade=' . ($ugrade['grade'] + 1))->find();
        $data = array(
            'distant_growth' => $higher_grade['floor_growth'] - $user['total_integral'],
            'grade_name' => $ugrade['grade_name'],
            'grade_icon' => $ugrade['grade_icon'],
            'higher_grade_name' => $higher_grade['grade_name']
        );
        return $data;
    }
    
    
    
    

}

?>
